Page({
  data: {

    inputValue: "",
    inputFocus: true,
    searchHis: [],
    historyShow: false,
    clearShow: false, // 点叉了吗
    page: 1,
    height: 0,
    list: [],
    noResult: false, // 没有结果
    // 分页
    toBottom: false, //判断是否有下一页 是否到底了
    pageSize: 10,
    pageIndex: 1,
    place: '',
    anyWord: '',// 点击的时候缓存

  },

  // 事件触发函数
  clear() {

    this.setData({
      inputValue: '',
      clearShow: false,

    })
    this.data.anyWord = '';
    this._initSearch()
    console.log('小图标不配绑定事件？')
  },
  toIndex() {
    console.log("??")
    wx.switchTab({
      url: '../../index/index'
    })
  },
  toDetail(e) {
    const { item } = e.currentTarget.dataset
    console.log(item, '传值')
    let that = this;
    // 当传递的数据过多的时候 使用eventChannel的方式来传值
    wx.navigateTo({
      url: '/pages/repair/workDetail/detail',
      success: function (res) {
        res.eventChannel.emit('toWorkDetail', item)
        res.eventChannel.on('workDetailEnd', (res) => {
          that._initList();
          console.log(res, '点击传值了该刷新了')
        })
      }
    })
  },
  watchInput(event) {
    let juge = this.data.clearShow
    if (!juge) {
      this.setData({
        clearShow: true,
      })
    }
    // // 如果没有输入值 那么搜索结果和X都该消失
    // if (!event.detail.cursor) {
    //   this.setData({
    //     clearShow: false,
    //     list: [],

    //     pageIndex: 1,
    //     noResult: false,
    //   })
    // }
  },
  searchHis(event) {
    let obj = {}
    let value = event.currentTarget.dataset.item
    obj.anyWord = value
    this.setData({

      clearShow: true,
      inputValue: value,
      anyWord: value
    })
    this._initSearch(obj)
    console.log(event)
  },
  clearHis() {
    this._initSearch()
    this.setData({
      searchHis: [],
      noResult: false,
    })
    this.Store.setItem('history', [])
  },
  search(event) {
    // if (!event.detail.value) {
    //   wx.showToast({
    //     title: '请输入关键词',
    //     icon: 'none',
    //     duration: 2000,
    //   })
    //   return
    // }
    let obj = {}
    obj.anyWord = event.detail.value
    // 存入本地 并且关闭历史
    let searchHis = this.data.searchHis
    console.log(searchHis, '转为set之后')
    // 空值搜索不记录历史
    if (searchHis.length > 5) {
      searchHis.unshift(event.detail.value)
      searchHis.pop()
    } else {
      searchHis.unshift(event.detail.value)
    }
    searchHis = new Set(searchHis)
    console.log(searchHis, '去重结束了吗')
    let arr = [...searchHis]
    this.Store.setItem('history', arr)
    this.setData({
      historyShow: false,
      searchHis: arr,
      anyWord: obj.anyWord

    })
    this._initSearch()
    console.log(event.detail, '点击搜索的话', arr, '搜索历史')
  },
  // 过滤函数
  filterNull(obj) {
    for (let key in obj) {
      if (obj[key] || key == "coverPhotoUrl") {

      } else {
        obj[key] = '无数据'
      }
    }
  },
  filterStr(str) {
    let len = str.length
    let result = str
    if (len > 60) {
      result = str.slice(0, 60) + '...'
    }
    return result
  },
  // api 初次加载刷新数据
  _initSearch() {
    let params = {

    };
    params.keyValue = this.data.anyWord;
    let page = {};
    page.pageIndex = 1;
    page.pageSize = 10;
    // 初始化页码
    this.data.pageIndex = 1;
    this.data.pageSize = 10;
    this.axios.query(
      "RepairWorkOrder",
      page,
      params,
    ).then((res) => {
      if (res.code == 200) {
        if (res.data.rows.length) {
          let tempArr = []
          let arr = ["普通", "紧急", "很紧急"];
          tempArr = res.data.rows.map((item) => {
            item.fixTime = item.createdTime.slice(0, 10);
            item.isDownText = item.isDown ? "是" : "否";
            if (item.workOrderCode) {
              if (item.workOrderCode.length > 10) {
                item.fixOrder = item.workOrderCode.slice(0, 10) + '...';
              } else {
                item.fixOrder = item.workOrderCode
              }
            }
            if (item.description) {
              if (item.description.length > 23) {
                item.fixDes = item.description.slice(0, 23) + '...';
              } else {
                item.fixDes = item.description
              }
            }
            if (item.contacts) {
              if (item.contacts.length > 2) {
                item.fixContacts = item.contacts.slice(0, 2) + '...';
              } else {
                item.fixContacts = item.contacts
              }
            }

            if (item.priority) {
              item.proText = arr[Number(item.priority)];
            } else {
              item.proText = '无状态'
            }
            return item;
          });
          console.log(tempArr)
          // 判断是否还有下一页
          this.data.pageMax = Math.round(res.data.total / 10);
          //this.data.toBottom = 10 >= res.data.total ? true : false;
          this.setData({
            list: tempArr
          })
          // 停止下拉刷新 也可以写个参数判定是否是下拉
          wx.stopPullDownRefresh()
        } else {
          this.setData({
            list: []
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000,
        })
      }
      console.log(res, '查询信息')
    })

  },
  // 下拉加载函数
  _search() {
    const { list, pageSize, pageIndex } = this.data
    let params = {};
    params.keyValue = this.data.anyWord
    console.log(this.data.anyWord, '当点击的时候缓存一个搜索值')
    let page = {};
    page.pageIndex = pageIndex;
    page.pageSize = pageSize;
    this.axios.query(
      "RepairWorkOrder",
      page,
      params,
    ).then((res) => {
      if (res.code == 200) {
        if (res.data.rows.length) {
          let arr = ["普通", "紧急", "很紧急"];
          let cache = res.data.rows.map((item) => {
            item.fixTime = item.createdTime.slice(0, 10);
            item.isDownText = item.isDown ? "是" : "否";
            if (item.workOrderCode) {
              if (item.workOrderCode.length > 10) {
                item.fixOrder = item.workOrderCode.slice(0, 10) + '...';
              } else {
                item.fixOrder = item.workOrderCode
              }
            }
            if (item.description) {
              if (item.description.length > 23) {
                item.fixDes = item.description.slice(0, 23) + '...';
              } else {
                item.fixDes = item.description
              }
            }
            if (item.contacts) {
              if (item.contacts.length > 2) {
                item.fixContacts = item.contacts.slice(0, 2) + '...';
              } else {
                item.fixContacts = item.contacts
              }
            }
            if (item.priority) {
              item.proText = arr[Number(item.priority)];
            } else {
              item.proText = '无状态'
            }

            return item;
          });
          let tempArr = []
          tempArr = list.concat(cache);
          this.data.toBottom = false
          this.setData({
            list: tempArr
          })
        }
      }
      console.log(res, '查询信息')
    })

  },
  //数据过滤函数
  toBottom(e) {
    let juge = this.data.toBottom
    let pageIndex = ++this.data.pageIndex
    let pageMax = this.data.pageMax;
    if(juge){
      return
    }
    if (pageIndex > pageMax) {
      wx.showToast({
        title: '已经到底了',
        icon: 'none',
        duration: 2000,
      })

    } else {
      this.data.toBottom = true
      this.setData({
        pageIndex: pageIndex
      })
      this._search()
    }
    console.log("上拉刷新会怎么样 那个logo", e)
  },
  // 生命周期函数
  init() {
    let that = this
    let history = this.Store.getItem('history') || []
    this.setData({
      searchHis: history
    })
    wx.getSystemInfo({
      success(res) {
        let multiple = 750 / res.windowWidth
        let height = Math.floor(multiple * res.windowHeight) - 120;
        that.setData({
          height: height + 'rpx'
        })
        console.log(that.data.height, '比较后高度')
      }
    })
    this._initSearch();
  },
  onLoad() {
    console.log("???")
    this.init()
  },
  onUnload() {
    let searchHis = this.data.searchHis
    this.Store.setItem('history', searchHis)
  },
  // 下拉刷新 使用需要去json里配置对应enablePullDownRefresh属性
  onPullDownRefresh() {
    this.data.anyWord = '';
    this._initSearch();
  },
})